package easamples;

import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.sparx.Collection;
import org.sparx.Element;
import org.sparx.Repository;


public class ManageElementsExample implements IExample
{
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Add and Manage Elements";
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#getDescription()
	 */
	public String getDescription()
	{
		//return "Add and delete Elements in a package";
		return "Example illustrating how to add and delete Elements in a Package.<br/><br/><b>NOTE:</b> Requires a Package ID for the Package to work in, so try running <code>Dump Model</code> first and note a suitable Package ID down.<br/><br/><b>Related APIs</b><ul><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?element2.htm'>Element API</a></li></ul>";
	}

	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#runExample(java.io.File, javax.swing.DefaultListModel)
	 */
	public void runExample( File projectFile, ExampleUI uiFrame )
	{
		Repository rep = null;
		DefaultListModel outputList = uiFrame.getOutputListModel();
		
		// Prompt the user for a Package ID to work on
		short packageID = ExampleHelpers.promptForShortID( 
				"Enter a Package ID to work on", uiFrame);
		
		if ( packageID < 0 )
		{
			outputList.addElement( "ManageElements Example cancelled by user" );
			return;
		}
		
		try
		{
			// Create a repository object - This will create a new instance of EA
			rep = new Repository();
			
			// Attempt to open the provided file
			if ( rep.OpenFile(projectFile.getAbsolutePath()) )
			{
				// Get the requested package
				org.sparx.Package thePackage = rep.GetPackageByID( packageID );
				Collection<Element> elements = thePackage.GetElements();
				
				// Create a new element in the package
				Element testElement = thePackage.GetElements().AddNew( "Login to Website", "UseCase" );
				testElement.SetStereotype( "testcase" );
				testElement.Update();
				thePackage.GetElements().Refresh();
				
				int testElementID = testElement.GetElementID();
				outputList.addElement( "Added Element: " + testElement.GetName() 
						+ " (ID=" + testElementID + ")");
				
				testElement = null;
								
				// Navigate the elements collection. Delete the newly added element when we find it
				for ( short i = 0 ; i < elements.GetCount() ; i++ )
				{
					Element currentElement = elements.GetAt( i );
					int currentElementID = currentElement.GetElementID();
					outputList.addElement( "Element: " + currentElement.GetName() );
					
					// If the element is the one we just added, then delete it
					if ( currentElementID == testElementID )
					{
						elements.DeleteAt( i, false );
						outputList.addElement( "Deleted Element: " + currentElement.GetName() + " (ID=" + currentElementID + ")" );
					}
				}
				
				outputList.addElement( "Done!" );
			}
			else
			{
				// If the file couldn't be opened then notify the user
				String message = "EA was unable to open the file '";
				message += projectFile.getAbsolutePath();
				message += "'.";
				JOptionPane.showMessageDialog( uiFrame, message, "Invalid File", JOptionPane.ERROR_MESSAGE );
				outputList.addElement( message );
			}
		}
		catch ( UnsatisfiedLinkError ule )
		{
			String message = "Unable to communicate with EA.\nPlease ensure that the path to SSJavaCOM.dll included in java.library.path";
			JOptionPane.showMessageDialog( uiFrame, message, "Unable to link to SSJavaCOM.dll", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		catch ( Exception e )
		{
			String message = "An unhandled exception was thrown: " + e.getMessage();
			JOptionPane.showMessageDialog( uiFrame, message, "Unhandled Exception", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		finally
		{
			if ( rep != null )
			{
				// Clean up
				rep.CloseFile();
				rep.Exit();
				rep.destroy();
			}
		}
	}

}
